package com.example.wuqujingdianphone.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.api.GlobalConfig;
import com.example.wuqujingdianphone.base.BaseActivity;
import com.example.wuqujingdianphone.bean.InfoBean;
import com.example.wuqujingdianphone.bean.LoginBean;
import com.example.wuqujingdianphone.utils.SPUtil;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MessageModifyActivity extends BaseActivity {
    private static final String TAG = "MessageModifyActivity";
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.username)
    EditText mUsername;
    @BindView(R.id.nickname)
    EditText mNickname;
    @BindView(R.id.names)
    EditText mNames;
    @BindView(R.id.email)
    EditText mEmail;
    @BindView(R.id.phone)
    EditText mPhone;
    @BindView(R.id.address)
    EditText mAddress;
    @BindView(R.id.ok)
    Button mOk;
    private String accout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_modify);
        ButterKnife.bind(this);

        mTitle.setText("信息修改");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        accout = SPUtil.getString(MessageModifyActivity.this, GlobalConfig.ACCOUNT_KEY, "");
        getModifyInfo();

        initData();


    }

    private void getModifyInfo() {
        //http://wenqujingdian.com/index.php/Jiekou/getinf/username/123
        ViseHttp.GET("index.php/Jiekou/getinf/username/" + accout)
                .tag(TAG)
                .request(new ACallback<InfoBean>() {
                    @Override
                    public void onSuccess(InfoBean result) {
                        // Log.i(TAG, "onSuccess: result=" + result);
                        mUsername.setText(result.username);
                        mNickname.setText(result.nicheng);
                        mNames.setText(result.xingming);
                        mEmail.setText(result.email);
                        mPhone.setText(result.tel);
                        mAddress.setText(result.address);

                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        //请求失败，errCode为错误码，errMsg为错误描述
                        ToastUtil.showToast("errCode=" + errCode + " errMsg=" + errMsg);
                    }
                });


    }

    private void initData() {

        //http://wenqujingdian.com/index.php/Jiekou/saveinf
        mOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isLegal()) {

                    ViseHttp.GET("index.php/Jiekou/saveinf/")
                            .tag(TAG)
                            .addParam("username", accout)
                            .addParam("nicheng", strNickname)
                            .addParam("xingming", strNames)
                            .addParam("email", strEmail)
                            .addParam("tel", strPhone)
                            .addParam("address", strAddress)
                            .request(new ACallback<LoginBean>() {
                                @Override
                                public void onSuccess(LoginBean result) {
                                    //Log.i(TAG, "onSuccess: result=" + result);
                                    if (result != null) {
                                        if (result.zhuangtai.equals("1")) {
                                            ToastUtil.show("修改成功");
                                            finish();
                                        } else {
                                            ToastUtil.show(result.message);
                                        }
                                    } else {
                                        ToastUtil.show("修改失败!");
                                    }

                                }

                                @Override
                                public void onFail(int errCode, String errMsg) {
                                    //请求失败，errCode为错误码，errMsg为错误描述
                                    ToastUtil.showToast("errCode=" + errCode + " errMsg=" + errMsg);
                                }
                            });


                }

            }
        });


    }

    //private String strUsername;
    private String strNickname;
    private String strNames;
    private String strEmail;
    private String strPhone;
    private String strAddress;

    private boolean isLegal() {

        //strUsername = mUsername.getText().toString().trim();
        strNickname = mNickname.getText().toString().trim();
        strNames = mNames.getText().toString().trim();
        strEmail = mEmail.getText().toString().trim();
        strPhone = mPhone.getText().toString().trim();
        strAddress = mAddress.getText().toString().trim();

        /*if (TextUtils.isEmpty(strUsername)) {
            ToastUtil.show("用户名不能为空");
            return false;
        }*/
        if (TextUtils.isEmpty(strNickname)) {
            ToastUtil.show("昵称不能为空");
            return false;
        }
        if (TextUtils.isEmpty(strNames)) {
            ToastUtil.show("真实姓名不能为空");
            return false;
        }

        if (TextUtils.isEmpty(strEmail)) {
            ToastUtil.show("邮箱不能为空");
            return false;
        }
        if (TextUtils.isEmpty(strPhone)) {
            ToastUtil.show("手机号不能为空");
            return false;
        }
        if (TextUtils.isEmpty(strAddress)) {
            ToastUtil.show("地址不能为空");
            return false;
        }


        return true;
    }


}
